package com.xyy.saas.payment.controller;

import com.alibaba.fastjson.JSONObject;
import com.xyy.saas.payment.dao.model.BankBillJd0026QueryParam;
import com.xyy.saas.payment.dao.model.BankBillJd0027QueryParam;
import com.xyy.saas.payment.dao.model.BankBillJd0028QueryParam;
import com.xyy.saas.payment.dao.model.BankCardBindQueryParam;
import com.xyy.saas.payment.exception.PaymentException;
import com.xyy.saas.payment.manage.cores.vo.BankCardBindLogVo;
import com.xyy.saas.payment.service.JDPayService;
import com.xyy.saas.payment.trace.AlarmService;
import com.xyy.saas.payment.trace.DingTalkMessage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;

@RestController
@RequestMapping("/jd")
@Slf4j
public class JDPayBillController {
    @Autowired
    private JDPayService jdPayService;
    @Autowired
    private AlarmService alarmService;

    /** ********************** 绑卡信息  **********************/
    @PostMapping(value = "/page_card", produces = "application/json;charset=utf-8")
    public String pageCard(@RequestBody BankCardBindQueryParam param) {
        log.info("page_card:{}", JSONObject.toJSONString(param));
        return JSONObject.toJSONString(jdPayService.queryCardPage(param));
    }
    @PostMapping(value = "/query_card_log", produces = "application/json;charset=utf-8")
    public String queryCardLog(@RequestBody BankCardBindLogVo param) throws IllegalAccessException {
        log.info("queryCardLog:{}", JSONObject.toJSONString(param));
        return JSONObject.toJSONString(jdPayService.queryCardLog(param));
    }

    /** ********************* 账单列表查询 **********************/
    @PostMapping(value = "/page_0026", produces = "application/json;charset=utf-8")
    public String bill0026Page(@RequestBody BankBillJd0026QueryParam param) throws PaymentException {
        log.info("bill0026Page:{}", JSONObject.toJSONString(param));
        return JSONObject.toJSONString(jdPayService.queryByPage0026(param));
    }
    @PostMapping(value = "/export_0026")
    public void export0026(@RequestBody BankBillJd0026QueryParam param, HttpServletRequest request, HttpServletResponse response) throws IOException, ParseException {
        log.info("导出0026对账单数据:{}",JSONObject.toJSONString(param));
        jdPayService.export0026(param, request, response);
    }

    @PostMapping(value = "/page_0027", produces = "application/json;charset=utf-8")
    public String bill0027Page(@RequestBody BankBillJd0027QueryParam param) throws PaymentException {
        log.info("bill0026Page:{}", JSONObject.toJSONString(param));
        return JSONObject.toJSONString(jdPayService.queryByPage0027(param));
    }
    @PostMapping(value = "/export_0027")
    public void export0027(@RequestBody BankBillJd0027QueryParam param, HttpServletRequest request, HttpServletResponse response) throws IOException, ParseException {
        log.info("导出0027对账单数据:{}",JSONObject.toJSONString(param));
        jdPayService.export0027(param, request, response);
    }

    @PostMapping(value = "/page_0028", produces = "application/json;charset=utf-8")
    public String bill0028Page(@RequestBody BankBillJd0028QueryParam param) throws PaymentException {
        log.info("bill0028Page:{}", JSONObject.toJSONString(param));
        return JSONObject.toJSONString(jdPayService.queryByPage0028(param));
    }
    @PostMapping(value = "/export_0028")
    public void export0028(@RequestBody BankBillJd0028QueryParam param, HttpServletRequest request, HttpServletResponse response) throws IOException, ParseException {
        log.info("导出0028对账单数据:{}",JSONObject.toJSONString(param));
        jdPayService.export0028(param, request, response);
    }

    /** ********************* 账单 **********************/
    @PostMapping(value = "/downBill", produces = "application/json;charset=utf-8")
    public String downBill(@RequestParam(value = "billDate") String billDate) throws Exception {
        jdPayService.downAndAnalysisBill("jdBill_person_" + billDate, billDate);
        return "success";
    }
    @PostMapping(value = "/submitBill", produces = "application/json;charset=utf-8")
    public String submitBill(@RequestParam(value = "merchantNo") String merchantNo, @RequestParam(value = "billDate") String billDate) throws Exception {
        return jdPayService.submitBillPingan("jdBill_person_" + billDate, 0L, merchantNo, billDate);
    }
    @PostMapping(value = "/dealBill", produces = "application/json;charset=utf-8")
    public String dealBill(@RequestParam(value = "billDate") String billDate) {
        String msg = jdPayService.billDeal("jdBill_person_" + billDate, billDate, true);
        if (StringUtils.isNotBlank(msg)) {
            DingTalkMessage talkMessage = DingTalkMessage.builder().type("京东绑卡支付").method("com.xyy.saas.payment.service.JDPayService.billDeal").msg(msg).param("").build();
            alarmService.alarm(talkMessage);
        }
        return "success";
    }

}
